Repository navigation
fix(session): accept ES256 access tokens and ignore unknown JWKS keys - #788
Conversation
The session verifier accepted only RS256 access tokens. It now accepts RS256 and ES256. The allow-list is one private constant. The token header, the JWKS and the caller cannot add an algorithm to it. The SDK now makes these checks before PyJWT verifies the signature: - The algorithm of the token is RS256 or ES256. - The token has no "crit" header (RFC 7515, section 4.1.11). - RS256 uses an RSA key. ES256 uses an EC key on the P-256 curve. - If the key has an "alg" member, it is equal to the token algorithm. - If the key has a "use" member, it is "sig". authenticate() now returns INVALID_JWT when the JWKS has no usable key for the "kid" of the token. It returns the same result when the key does not agree with the token algorithm, and when PyJWT cannot read the JWKS. Before this change, authenticate() raised an error for these conditions. For some of them, refresh() returned the text of the error. refresh() now returns INVALID_JWT. PyJWT does not ignore three types of JWKS key: a key with "alg": "none", a key whose "alg" is an array or an object, and an "oct" key without "k". For a JWKS with such a key, the SDK returns INVALID_JWT for each token. The result for a JWKS request failure does not change. authenticate() raises the error of the request. The docstring of authenticate() now shows this error. The JWKS request now sends the User-Agent of the SDK. The minimum PyJWT version is now 2.15. That version ignores most JWKS keys that it cannot load and limits the JWKS requests for an unknown "kid". The tests run for Session and AsyncSession, with authenticate() and refresh(). They make sure of these conditions: - RS256 and ES256 tokens verify with RSA and EC keys in each order. - OKP, AKP and unknown key types in the JWKS have no effect. - Keys without the optional members verify tokens. - Algorithm confusion, a P-384 key, an unknown "kid", a "crit" header and an incorrect ES256 signature give INVALID_JWT. - A JWKS that PyJWT cannot read gives INVALID_JWT. - A JWKS request failure gives the same result as before.
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge; no actionable new issue was found. SummaryAdds ES256 access-token support while keeping RS256 support.
No actionable new issue was found. Tests were not run during this review.
|
nicknisi
left a comment
There was a problem hiding this comment.
Reviewed by Riker for Nick, at this head. Checked: the accepted algorithms are exactly RS256 and ES256 (HS* and none stay rejected), the key is matched to the token's algorithm and key type, unknown or unusable JWKS entries don't break verification of valid tokens, RS256 behavior is unchanged, and the new tests cover those cases.
Non-blocking follow-up: read the JWKS per entry and select by kid and key type. Today a few odd entry shapes fail every token, and when an RSA and an EC key share a kid only the first-listed one works. It fails closed, so not a security problem.
…id and algorithm PyJWKClient reads the JWKS as one document. A key that PyJWT cannot read, for example a key with "alg": "none", makes the whole document unusable, and no token verifies. PyJWKClient also selects a key by its kid only. When an RSA key and an EC key have the same kid, only the first key in the JWKS verifies tokens. The SDK now uses a PyJWKClient subclass that reads each key on its own and ignores the keys that PyJWT cannot read. It selects the key that has the kid of the token and is a key for the algorithm of the token. The client keeps the keys for the lifespan, and gets the JWKS again for an unknown kid one time at most in each cooldown period, as before.
Description
The session verifier accepted only RS256 access tokens. With this change, the verifier accepts RS256 and ES256. RS256 tokens verify as before.
Changes
algmember, it must be equal to the token algorithm. If the key has ausemember, it must besig.critheader (RFC 7515, section 4.1.11)."alg": "none", has no effect on the other keys.kidof the token and the algorithm of the token. When an RSA key and an EC key have the samekid, each token gets the key for its algorithm.authenticate()returnsINVALID_JWTwhen the JWKS has no key that has thekidof the token and is a key for its algorithm. It returns the same result when PyJWT can read no key of the JWKS. Before this change,authenticate()raised an error for these conditions.refresh()returnsINVALID_JWTfor the same conditions. Before this change,refresh()returned the text of the error for some of them.User-Agentof the SDK.Limit
If PyJWT can read no key of the JWKS, the SDK returns
INVALID_JWTfor each token and does not keep the JWKS. It does not raise an error.Dependency
The minimum PyJWT version changes from 2.12 to 2.15 (
pyjwt~=2.15). The JWKS client of the SDK is aPyJWKClient. It uses thecooldown_durationof PyJWT 2.15 to limit the JWKS requests for an unknownkidto one in each 30 seconds. PyJWT 2.15 supports Python 3.9 and later, thus the supported Python versions do not change. The SDK reads the JWKS and makes the algorithm and key checks itself. It does not rely on the PyJWT version for them.No change
authenticate()raisesPyJWKClientConnectionErrorandrefresh()returnsINVALID_JWT. If the connection stops in the body of the response,authenticate()raisesOSError. The docstring ofauthenticate()now shows these errors.Code that replaces
session.jwksThe SDK now gives the
PyJWKobject tojwt.decode. A replacement forsession.jwksmust return ajwt.PyJWKobject fromget_signing_key_from_jwt(). A mock object that has only akeymember now givesINVALID_JWT. A replacement can select the key by itskidonly, asPyJWKClientdoes. The SDK then makes sure that the key is a key for the algorithm of the token.Tests
The new tests run for
SessionandAsyncSession, withauthenticate()andrefresh(). The tests make each JWK and each signature without PyJWT. They make all keys at test time."alg": "none", a key whosealgis an array or an object, and anoctkey withoutk.kidverify RS256 and ES256 tokens, in each order of the keys. The SDK also goes past a key with thatkidthat is not a key for the algorithm.alg,use,x5candx5t#S256verify tokens.INVALID_JWT:kidof the EC key, and an ES256 header with thekidof the RSA keynone, and each other algorithmkidcritheaderINVALID_JWT. The tests use a JWKS with only keys that PyJWT cannot read, a body that is not JSON and a body with too many levels.kid, it gets the JWKS again one time at most in each cooldown period. The tests move a replaced clock.PyJWKClientverifies tokens. The SDK refuses the key that it gives when the key is not a key for the algorithm of the token.kid. The client read the keys, selected the key for each algorithm, made one request and sent the SDKUser-Agent.jwt.decode.PyJWKobject, asPyJWKClientdoes../scripts/ciis successful: format, lint, type check and 4493 tests.Documentation
Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.
If yes, link a related docs PR and add a docs maintainer as a reviewer. Their approval is required.